﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TileMapEditor;

namespace TileMapEditor
{
    public class EnumParser
    {
        public static int[] GetEnumValues(System.Type enumType)
        {
            var arr = System.Enum.GetValues(enumType);

            int[] ret = new int[arr.Length];
            for (int i = 0; i < arr.Length; i++)
            {
                ret[i] = (int)arr.GetValue(i);
            }

            return ret;
        }

        public static string[] GetEnumDescriptions(System.Type enumType)
        {
            var arr = System.Enum.GetValues(enumType);

            string[] ret = new string[arr.Length];
            for (int i = 0; i < arr.Length; i++)
            {
                string fieldName = System.Enum.GetName(enumType, arr.GetValue(i));
                System.Reflection.FieldInfo fieldInfo = arr.GetValue(i).GetType().GetField(fieldName);
                object[] attr = fieldInfo.GetCustomAttributes(false);
                ret[i] = ((EnumDescriptionAttribute)attr[0]).Description + "(" + (int)arr.GetValue(i) + ")";
            }

            return ret;
        }

        public static string GetEnumValueDes(System.Type enumType, int val)
        {
            var arr = System.Enum.GetValues(enumType);

            string[] ret = new string[arr.Length];
            for (int i = 0; i < arr.Length; i++)
            {
                if( (int)arr.GetValue(i) == val)
                {
                    string fieldName = System.Enum.GetName(enumType, arr.GetValue(i));
                    System.Reflection.FieldInfo fieldInfo = arr.GetValue(i).GetType().GetField(fieldName);
                    object[] attr = fieldInfo.GetCustomAttributes(false);
                    return ((EnumDescriptionAttribute)attr[0]).Description;
                }
            }
            return "";
        }
    }

    public class EnumDescriptionAttribute : System.Attribute
    {
        private string m_strDescription;
        public EnumDescriptionAttribute(string strPrinterName)
        {
            m_strDescription = strPrinterName;
        }

        public string Description
        {
            get { return m_strDescription; }
        }
    }

}